<template>
  <div
    class="relative flex flex-col w-full justify-center items-center"
    :class="[
      'mt-' + (info.extension.topMargin && info.extension.topMargin.value),
      'mr-' + (info.extension.rightMargin && info.extension.rightMargin.value),
      'mb-' + (info.extension.bottomMargin && info.extension.bottomMargin.value),
      'ml-' + (info.extension.leftMargin && info.extension.leftMargin.value),
    ]"
    :style="{
      '--height': `${info.extension.boxHeight.value}px`,
      height: `${info.extension.boxHeight.value}px`,
      backgroundRepeat: 'no-repeat',
      backgroundSize: 'cover',
      backgroundColor: 'rgba(0,0,0,0)',
    }"
  >
    <div
      class="box_stream flex justify-center items-center"
      :style="{
        color: info.extension.color.value,
        width: info.extension.width.value + 'px',
        height: info.extension.height.value + 'px',
        '--leftColor': info.extension.leftColor.value,
        '--rightColor': info.extension.rightColor.value,
        '--topColor': info.extension.topColor.value,
        '--bottomColor': info.extension.bottomColor.value,
        '--weight': info.extension.weight.value + 'px',
      }"
    >
      <div
        :style="{
          fontSize: info.extension.fontSize?.value + 'px',
        }"
      >
        {{ info.extension.isDesign == undefined || info.extension.isDesign?.value ? info.extension.title.value : text }}
      </div>
      <div
        v-if="info.extension.subTitle?.value"
        :style="{
          fontSize: info.extension.subFontSize?.value + 'px',
          margin: '5px 0 0',
        }"
      >
        {{ info.extension.subTitle?.value }}
      </div>
    </div>
  </div>
</template>
<script>
  import { defineComponent } from 'vue'
  import { UiMixin, RefsInjectMixin, ChartMixin } from '@ve/mixin'
  import cloneDeep from 'lodash/cloneDeep'
  import { getModelInfo } from '@ve/utils/getModelInfo'
  export default defineComponent({
    name: 'UiRotateCard',
    mixins: [UiMixin, RefsInjectMixin, ChartMixin],
    inject: {
      hasPreview: { default: false },
    },
    props: {
      info: {
        required: true,
        type: Object,
        default: function () {
          return {}
        },
      },
    },
    data() {
      return {
        infoComponent: {},
        inter: null,
        text: '',
      }
    },
    watch: {
      info: {
        handler() {
          this.$nextTick(() => {
            console.log(getModelInfo(this, this.info._id))
            this.infoComponent = cloneDeep(this.info)
            this.getRequestData()
            this.inter && (clearInterval(this.inter), (this.inter = null))
            if (this.info.extension.requestSet.value.loopRequest) {
              this.inter = setInterval(() => {
                this.getRequestData()
              }, this.info.extension.requestSet.value.loopTime)
            }
          })
        },
        deep: true,
      },
      set: {
        handler(val) {
          console.log(val, '-------')
          this.$forceUpdate()
        },
        deep: true,
      },
      // getSelectModule: {
      //   handler(val, oldVal) {
      //     if (!oldVal.extension) {
      //       if (pieOption[this.info.chartTypeValue]) {
      //         this.getSelectModule.extension && this.getSelectModule.extension.series && (this.getSelectModule.extension.series.isShowBtn = false)
      //       } else {
      //         this.getSelectModule.extension && this.getSelectModule.extension.series && (this.getSelectModule.extension.series.isShowBtn = true)
      //       }
      //     }
      //   },
      //   deep: true,
      // },
    },
    created() {
      this.infoComponent = cloneDeep(this.info)
    },
    mounted() {
      this.$nextTick(() => {
        this.getRequestData()
        if (this.info.extension.requestSet.value.loopRequest) {
          this.inter = setInterval(() => {
            this.getRequestData()
          }, this.info.extension.requestSet.value.loopTime)
        } else {
          this.inter && (clearInterval(this.inter), (this.inter = null))
        }
      })
    },
    deactivated() {
      console.warn('退出----------------------')
      this.inter && clearInterval(this.inter)
    },
    beforeUnmount() {
      this.inter && clearInterval(this.inter)
    },
    methods: {
      getRequestData() {
        if (this.info.extension.isDesign?.value == false) {
          this.$request(this.infoComponent.extension.requestSet.value, (res) => {
            this.infoComponent.extension.requestSet?.value.cardValue &&
              (this.text = cloneDeep(res)[0][this.infoComponent.extension.requestSet.value.cardValue])
          })
        }
      },
    },
  })
</script>
<style>
  .uiRotateCard {
    height: var(--height);
  }
  .box_stream {
    position: relative;
    width: 100px;
    height: 100px;
    color: #fff;
    flex-direction: column;
  }
  .box_stream::after {
    content: '';
    display: inline-block;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    border-left: var(--weight) dashed var(--leftColor);
    border-right: var(--weight) dashed var(--rightColor);
    border-top: var(--weight) dashed var(--topColor);
    border-bottom: var(--weight) dashed var(--bottomColor);
    border-radius: 50%;
    animation: anit 15s;
    animation-iteration-count: infinite;
    animation-timing-function: linear;
    box-shadow: 0 0 20px -1px var(--leftColor);
    transition: all 15s;
  }
  @keyframes anit {
    from {
      transform: rotateZ(0);
      border-left: var(--weight) dashed var(--rightColor);
      border-right: var(--weight) dashed var(--leftColor);
      border-top: var(--weight) dashed var(--bottomColor);
      border-bottom: var(--weight) dashed var(--topColor);
    }
    to {
      transform: rotateZ(360deg);
      border-left: var(--weight) dashed var(--leftColor);
      border-right: var(--weight) dashed var(--rightColor);
      border-top: var(--weight) dashed var(--topColor);
      border-bottom: var(--weight) dashed var(--bottomColor);
    }
  }
</style>
